


package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.PointRule;
import com.jfinal.plugin.activerecord.Page;
import com.cy.model.PointLog;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Map;

/**
 * Created by Dio-Nbook on 7-8.
 */
@ControllerBind(controllerKey = "/clmp/pointLog")
public class PointLogController extends WebApiController {
    @Override
    public void index() {
        PointLog pointLog = getModel(PointLog.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();

        if (null != parasMap){
            String start = null ;
            if(null != parasMap.get("start_time")){
                start = parasMap.get("start_time")[0];
            }
            String end = null;
            if (null != parasMap.get("end_time")){
                end = parasMap.get("end_time")[0];
            }
            if (StringUtils.isNotBlank(start)){
                pointLog.put("start",start);
            }
            if (StringUtils.isNotBlank(end)){
                pointLog.put("end",end);
            }
        }
        Page<PointLog> pointLogPage = PointLog.dao.paginate(page, pageSize, pointLog);
        renderBuiPage(pointLogPage, 200);
    }

    @Override
    public void show() {

    }

   @Override
    public void update() {
        int id = getParaToInt();
        PointLog tempPointLog = getModel(PointLog.class);
        PointLog pointLog = PointLog.dao.findById(id);
        pointLog.cover(tempPointLog);

        if (pointLog.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        PointLog pointLog = getModel(PointLog.class);
        if (pointLog.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            PointLog.dao.deleteById(id);
        }
        renderSuccess();
    }

   /* public void getTodayLog(){
        Integer [] ruleList = getParaValuesToInt("rules");
        //目前 只找今日完成的任务
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);

        Calendar end = Calendar.getInstance();

        int unCompany = 0;
        for(Integer id : ruleList){
            PointLog sum = PointLog.dao.sumLogActionTimes(id,todayStart.getTime(),end.getTime(),getLoginUser().getUserId());
            if (null != sum && null != sum.get("action_sum") && rule.getActionCount() <= sum.getLong("action_sum")){
                rule.put("isCompany",true);
            }else {
                unCompany++;
            }
        }
    }*/
}
